package com.ocom.payconfig.controller;

import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfGoodsListProcess;
import com.ocom.common.entity.card.EntityXfRecordProcess;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.entity.payconfig.EntityMealPlan;
import com.ocom.common.entity.payconfig.EntityReceiptAddress;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.mealorder.*;
import com.ocom.common.request.ugcapi.OrderedShowRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.client.XfRecordFeign;
import com.ocom.payconfig.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 点餐 消费配置 11.4 点餐
 * @Author: 朱晖
 * @Date: 2023/5/15
 **/
@Slf4j
@RestController
public class MealOrderController {


	@Autowired
	MealPlanService mealPlanService;

	@Autowired
	MealMenuService mealMenuService;

	@Autowired
	XfGoodsListProcessService xfGoodsListProcessService;

	@Autowired
	XfRecordProcessService xfRecordProcessService;

	@Autowired
	PersonnelInfoService personnelInfoService;

	@Autowired
	ReceiptAddressService receiptAddressService;

	@Autowired
	DishesPriceService dishesPriceService;

	@Autowired
	XfRecordFeign xfRecordFeign;

	/**
	 * 11.4.1排餐列表查询（getOrderPlanDishes）
	 */
	@PostMapping(PayConfigUrl.Url_getOrderPlanDishes)
	public Result getOrderPlanDishes(@RequestBody GetOrderPlanDishesRequest request) {

		EntityMealPlan entityMealPlan = mealPlanService.upToDateMealPlanInfo(
				request.getComId(),
//				request.getMerchId(),
				request.getPointId(),
				request.getReceiptId(),
				request.getMealDate()
		);
		if (entityMealPlan == null) {
			return ResultUtil.success(new ArrayList<>());
		}

		List<GetOrderDishesInfoDao> dishesInfoDaos = mealMenuService.getDishesInfo(request.getComId(),entityMealPlan.getMealplanId(),entityMealPlan.getDays(),request.getMealNo());

		List<GetOrderPlanDishesResponse> responseList = new ArrayList<>();

		for (GetOrderDishesInfoDao dishesInfoDao : dishesInfoDaos) {
			GetOrderPlanDishesResponse response = new GetOrderPlanDishesResponse();
			response.setMealMenuId(entityMealPlan.getMealplanId());
			response.setPointId(entityMealPlan.getPointId());
			response.setMerchId(entityMealPlan.getMerchId());
			response.setMealNo(dishesInfoDao.getMealNo());
			response.setDishesId(dishesInfoDao.getDishesId());
			response.setDishesName(dishesInfoDao.getDishesName());
			response.setPhotoUrl(dishesInfoDao.getPhotoUrl());
			response.setRemark(dishesInfoDao.getRemark());
			response.setDishesClassId(dishesInfoDao.getDishesClassId());
			response.setSort(dishesInfoDao.getSort());
			response.setPriceInfo(dishesInfoDao.getPriceInfo());
			response.setQuotaMax(dishesInfoDao.getQuotaMax());
			response.setQuotaMin(dishesInfoDao.getQuotaMin());
			response.setDishesSort(dishesInfoDao.getDishesSort());

			if (StringUtils.isNotBlank(dishesInfoDao.getPriceInfo())) {
				try {
					List<GetOrderPlanDishesResponse.Price> priceList = new ArrayList<>();

					String[] price1 = dishesInfoDao.getPriceInfo().split(";");
					for (String str : price1) {
						if (StringUtils.isNotBlank(str)) {
							GetOrderPlanDishesResponse.Price price = new GetOrderPlanDishesResponse.Price();
							String[] price2 = str.split(",");
							price.setPriceMode(price2[0]);
							price.setDishesPrice(price2[1]);
							price.setDishesPriceNew(price2[2]);
							price.setLimitedQuantity(price2[3]);
							priceList.add(price);
						}
					}
					response.setPriceList(priceList);
				}catch (Exception e) {
					e.printStackTrace();
				}
			}

			responseList.add(response);
		}

		log.info("排餐列表查询 ,【{}】【{}】【{}】",request,dishesInfoDaos,responseList);

		return ResultUtil.success(responseList);
	}

	/**
	 * 11.4.2商户、消费点列表（getMerchPointList）
	 */
	@PostMapping(PayConfigUrl.Url_getMerchPointList)
	public Result getMerchPointList(@RequestBody GetMerchPointListRequest request) {
		return ResultUtil.success(mealPlanService.getMerchPointList(request.getComId(),
				request.getReceiptId(),
				request.getMealDate()));
	}

	/**
	 * 11.4.3已点餐查询（getOrderDishes）
	 */
	@PostMapping(PayConfigUrl.Url_getOrderDishes)
	public Result getOrderDishes(@RequestBody GetOrderDishesRequest request) {
		if(request.getPId()!=null && request.getPId()!=0l){
			List<Long> pids1= new ArrayList<>();
			pids1.add(request.getPId());
			request.setIds(pids1);
		}else{
			if(!CommonUtil.isNull(request.getPCode()) ||  !CommonUtil.isNull(request.getPName()) ){
				List<EntityPersonnelInfo> list = personnelInfoService.selectPerson(request.getComId(),request.getPCode(),request.getPName());
				if(list==null || list.size()==0){
					return ResultUtil.success(new ArrayList<>());
				}
				List<Long> pids1= list.stream().map(EntityPersonnelInfo -> EntityPersonnelInfo.getPId()).collect(Collectors.toList());
				request.setIds(pids1);
			}
		}
		if(!CommonUtil.isNull(request.getReceiptName())){
			List<EntityReceiptAddress> list1 = receiptAddressService.getReceiptIds(request.getComId(),request.getReceiptName());
			if(list1==null || list1.size()==0){
				return ResultUtil.success(new ArrayList<>());
			}
			List<Integer> receiptIds= list1.stream().map(EntityReceiptAddress -> EntityReceiptAddress.getReceiptId()).collect(Collectors.toList());
			request.setReceiptIds(receiptIds);
		}

		return xfRecordFeign.getOrderDishes(request);
	}

//	@PostMapping(PayConfigUrl.Url_getOrderDishesB)
//	public Result getOrderDishesB(@RequestBody GetOrderDishesRequest request) {
//		if(request.getPId()!=null && request.getPId()!=0l){
//			List<Long> pids1= new ArrayList<>();
//			pids1.add(request.getPId());
//			request.setIds(pids1);
//		}else{
//			if(!CommonUtil.isNull(request.getPCode()) ||  !CommonUtil.isNull(request.getPName()) ){
//				List<EntityPersonnelInfo> list = personnelInfoService.selectPerson(request.getComId(),request.getPCode(),request.getPName());
//				if(list==null || list.size()==0){
//					return ResultUtil.success();
//				}
//				List<Long> pids1= list.stream().map(EntityPersonnelInfo -> EntityPersonnelInfo.getPId()).collect(Collectors.toList());
//				request.setIds(pids1);
//			}
//		}
//		if(!CommonUtil.isNull(request.getReceiptName())){
//			List<EntityReceiptAddress> list1 = receiptAddressService.getReceiptIds(request.getComId(),request.getReceiptName());
//			if(list1==null || list1.size()==0){
//				return ResultUtil.success(new ArrayList<>());
//			}
//			List<Integer> receiptIds= list1.stream().map(EntityReceiptAddress -> EntityReceiptAddress.getReceiptId()).collect(Collectors.toList());
//			request.setReceiptIds(receiptIds);
//		}
//
//		return xfRecordFeign.getOrderDishesB(request);
//	}

	/**
	 * 11.4.4菜品下单（addOrderDishes）
	 */
	@PostMapping(PayConfigUrl.Url_addOrderDishes)
	public Result addOrderDishes(@RequestBody EntityXfGoodsListProcess entityXfGoodsListProcess) {
		List<EntityXfGoodsListProcess> list = new ArrayList<>();
		list.add(entityXfGoodsListProcess);
		Boolean bp = xfGoodsListProcessService.addXfGoodsListProcessList(entityXfGoodsListProcess.getComId(), list);
		if (!bp) {
			return ResultUtil.error(ResultEnum.Err_3043);
		}
		return ResultUtil.success();
	}

	/**
	 * 11.4.5退餐(refundOrderDishes)
	 */
	@PostMapping(PayConfigUrl.Url_refundOrderDishes)
	public Result refundOrderDishes(@RequestBody RefundOrderDishesRequest request) {
		EntityXfRecordProcess entityXfRecordProcess = xfRecordProcessService.selectTradeNo(request.getComId(), request.getOrderNo(), null);
		if (entityXfRecordProcess == null) {
			return ResultUtil.error(ResultEnum.Err_3036);
		}

		request.setMonthValue(entityXfRecordProcess.getMonthValue());

		xfGoodsListProcessService.refund(request.getComId(),request.getOrderNo());

		xfRecordFeign.refund(request);

		return ResultUtil.success();
	}

	@PostMapping(PayConfigUrl.Url_refundOrderDishesB)
	public Result refundOrderDishesB(@RequestBody RefundOrderDishesRequest request) {
		EntityXfRecordProcess entityXfRecordProcess = xfRecordProcessService.selectTradeNo(request.getComId(), request.getOrderNo(), null);
		if (entityXfRecordProcess == null) {
			return ResultUtil.error(ResultEnum.Err_3036);
		}

		request.setMonthValue(entityXfRecordProcess.getMonthValue());

		xfGoodsListProcessService.refund(request.getComId(),request.getOrderNo(),request.getGoodsId());

		xfRecordFeign.refund(request);

		return ResultUtil.success();
	}

	/**
	 * 11.4.6点餐餐段查询（getOrderPlanMeal）
	 */
	@PostMapping(PayConfigUrl.Url_getOrderPlanMeal)
	public Result getOrderPlanMeal(@RequestBody GetOrderPlanMealRequest request) {
		return ResultUtil.success(mealPlanService.getOrderPlanMeal(request.getComId(), request.getMealDate(),request.getTagIds(),request.getPointId()));
	}

	/**
	 * 11.4.8已点餐菜品查看（单个订单）（getOrderDishesDetail）
	 */
	@PostMapping(PayConfigUrl.Url_getOrderDishesDetail)
	public Result getOrderDishesDetail(@RequestBody OrderedShowRequest request) {
		return xfRecordFeign.getOrderDishesDetail(request);
	}

	@PostMapping(PayConfigUrl.URL_getXfGoodsList)
	public Result getXfGoodsList(@RequestBody JSONObject jsonObject){
		return ResultUtil.success(this.xfGoodsListProcessService.getXfGoodsList(jsonObject.getLong("comId"), jsonObject.getString("orderNo")));
	}

	@PostMapping(PayConfigUrl.URL_updateXfGoods)
	public Result updateGoods(@RequestBody EntityXfGoodsListProcess entityXfGoodsListProcess){
		return ResultUtil.success(this.xfGoodsListProcessService.updateById(entityXfGoodsListProcess.getComId(),entityXfGoodsListProcess));
	}
}
